<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="订单状态" prop="status">
<!--        订单状态 0 - 未支付   1 - 支付中  2 - 已支付  3 - 订单已完成 4 -  订单取消-->
        <el-select v-model="queryParams.status" placeholder="选择订单状态" clearable style="width:140px;" size="mini">
          <el-option label="未支付" :value="0" />
          <el-option label="待支付" :value="1" />
          <el-option label="已支付" :value="2" />
          <el-option label="订单已完成" :value="3" />
          <el-option label="订单取消" :value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable size="mini" style="width:120px;"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="mini" style="width:120px;"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="查询时间" prop="propertyName">
        <el-date-picker v-model="dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:mealOrder:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['module:mealOrder:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['module:mealOrder:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:mealOrder:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" width="150"/>
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户头像" align="center">
        <template slot-scope="scope">
          <a :href="scope.row.faceUrl" style="color: #42b983" target="_blank">
            <img :src="scope.row.faceUrl" alt="点击打开" class="el-avatar">
          </a>
        </template>
      </el-table-column>
      <el-table-column label="活动套餐" align="center" prop="mealTitle" />
      <el-table-column label="套餐规格" align="center" prop="mealInfoTitle" />
      <el-table-column label="订单总价" align="center" prop="totalPrice">
        <template slot-scope="scope">
          {{scope.row.totalPrice+'元'}}
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="payPrice" >
        <template slot-scope="scope">
          {{scope.row.payPrice+'元'}}
        </template>
      </el-table-column>
      <el-table-column label="套餐金额" align="center" prop="mealMoney" >
        <template slot-scope="scope">
          {{scope.row.mealMoney+'元'}}
        </template>
      </el-table-column>
      <el-table-column label="赠送金额" align="center" prop="givenMoney" >
        <template slot-scope="scope">
          {{scope.row.givenMoney+'元'}}
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="status" fixed="right">
        <template slot-scope="scope">
          <div>
            <!-- 订单状态 0 - 未支付   1 - 支付中  2 - 已支付  3 - 订单已完成 4 -  订单取消  5 - 已发起退款  6 - 同意退款  7 - 拒绝退款  8 - 退款完成 -->
            <el-tag v-if="scope.row.status === 0" style="cursor: pointer" :type="'danger'">未支付</el-tag>
            <el-tag v-if="scope.row.status === 1" style="cursor: pointer" :type="''">待支付</el-tag>
            <el-tag v-if="scope.row.status === 2" style="cursor: pointer" :type="'success'">已支付</el-tag>
            <el-tag v-if="scope.row.status === 3" style="cursor: pointer" :type="'success'">订单已完成</el-tag>
            <el-tag v-if="scope.row.status === 4" style="cursor: pointer" :type="'info'">订单取消</el-tag>
            <el-tag v-if="scope.row.status === 5" style="cursor: pointer" :type="'warning'">已发起退款</el-tag>
            <el-tag v-if="scope.row.status === 6" style="cursor: pointer" :type="''">同意退款</el-tag>
            <el-tag v-if="scope.row.status === 7" style="cursor: pointer" :type="'danger'">拒绝退款</el-tag>
            <el-tag v-if="scope.row.status === 8" style="cursor: pointer" :type="'success'">退款完成</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="赠品领取状态" align="center" prop="rewardVerify"  width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.isHasReward == 1">
            <el-tooltip placement="top" v-if="scope.row.rewardVerify === 0" >
              <div slot="content" v-html="scope.row.verifyContent"></div>
              <el-tag style="cursor: pointer" :type="'danger'">未领取</el-tag>
            </el-tooltip>

            <el-tooltip placement="top" v-if="scope.row.rewardVerify === 1" >
              <div slot="content" v-html="scope.row.verifyContent"></div>
              <el-tag style="cursor: pointer" :type="'success'">已领取</el-tag>
            </el-tooltip>
          </div>
          <div v-if="scope.row.isHasReward == 0">
            <el-tag style="cursor: pointer" :type="''">无赠品</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="createTime" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="130">
        <template slot-scope="scope">
          <!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['module:mealOrder:edit']">详情</el-button>-->
          <el-button size="mini" v-if="scope.row.status == 2 || scope.row.status == 3" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:mealOrder:edit']">核销赠品</el-button>
          <el-button size="mini" type="text" v-if="scope.row.status == 0 || scope.row.status == 1 || scope.row.status == 4"
                     icon="el-icon-delete" @click="handleDelete(scope.row)"
                      v-hasPermi="['module:mealOrder:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改套餐活动支付订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="套餐名称" prop="mealTitle">
              {{form.mealTitle}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购买套餐规格" prop="mealInfoTitle">
              {{form.mealInfoTitle}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单号" prop="orderNo">
              {{form.orderNo}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="额外订单号" prop="outTradeNo">
              {{form.outTradeNo}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              {{form.nickName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户姓名" prop="realName">
              {{form.realName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单总价" prop="totalPrice">
              {{form.totalPrice+'元'}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际支付金额" prop="payPrice">
              {{form.payPrice+'元'}}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="赠品领取状态" prop="rewardVerify">
              <el-radio-group v-model="form.rewardVerify" size="mini">
                <el-radio :value="1" :label="1">已领取</el-radio>
                <el-radio :value="0" :label="0">未领取</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="赠品领取备注信息" prop="verifyContent">
              <el-input v-model="form.verifyContent" type="textarea" placeholder="请输入赠品领取备注信息" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="管理员备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" size="mini"/>
            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/module/meal/order";

  export default {
    name: "Order",
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        dateRange:[],
        // 总条数
        total: 0,
        // 套餐活动支付订单表格数据
        orderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          beginDate:undefined,
          endDate:undefined,
          orderNo:undefined,
          nickName:undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询套餐活动支付订单列表 */
      getList() {
        this.loading = true;
        listOrder(this.queryParams).then(response => {
          this.orderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          propertyId: undefined,
          estateId: undefined,
          orderNo: undefined,
          outTradeNo: undefined,
          payNo: undefined,
          userId: undefined,
          mealId: undefined,
          mealInfoId: undefined,
          nickName: undefined,
          realName: undefined,
          phoneNum:undefined,
          totalNum: undefined,
          totalPrice: undefined,
          payPrice: undefined,
          mealMoney:undefined,
          givenMoney:undefined,
          fee: undefined,
          payTime: undefined,
          payType: undefined,
          transactionId: undefined,
          createTime: undefined,
          updateTime: undefined,
          status: 0,
          refundOutTradeNo: undefined,
          refundStatus: "0",
          refundReasonWapImg: undefined,
          refundReasonWapExplain: undefined,
          refundReasonTime: undefined,
          refundReasonWap: undefined,
          refundReason: undefined,
          refundPrice: undefined,
          useIntegral: undefined,
          mark: undefined,
          isDel: undefined,
          remark: undefined,
          isChannel: undefined,
          isHasReward: undefined,
          rewardVerify: undefined,
          verifyTime: undefined,
          verifyContent: undefined,
          verifyRemark: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        console.log('dateRange',this.dateRange);
        if(this.dateRange !=undefined && this.dateRange.length === 2){
          this.queryParams.beginDate = this.dateRange[0];
          this.queryParams.endDate = this.dateRange[1];
        }
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length!==1;
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加套餐活动支付订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids;
        getOrder(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改套餐活动支付订单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateOrder(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addOrder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除套餐活动支付订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delOrder(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有套餐活动支付订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
